package cp3120.net.services;

import cp3120.dao.DAOFactory;
import cp3120.model.Credentials;
import cp3120.model.User;
import cp3120.net.ActiveUser;
import cp3120.net.AirportServer;
import java.util.List;


/**
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class AccountManagerService {
    public User whoAmI() {
        return ActiveUser.getUser();
    }

    public List<User> activeUserList() {
        return AirportServer.getInstance().getActiveUsers();
    }

    public User authenticateUser(Credentials credentials) {
        User user = DAOFactory.getUserDAO().getUser(credentials.getUsername());
        
        // If the user dosent exist, or the passwords dont match then the
        // credentials are invalid.
        if(user == null) {
            System.out.println("[Accountman] Invalid username.");
            return null;
        }

        if(!user.getPassword().equals(credentials.getPassword())) {
            System.out.println("[Accountman] invalid password");
            return null;
        }

        if(AirportServer.getInstance().getActiveUsers().contains(user)) {
            System.out.println("[Accountman] User already logged in.");
            return null;
        }

        // On success make sure we update the user on this end.
        ActiveUser.setUser(user);

        return user;
    }
}
